﻿using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApp_d1
{
    public class Order
    {
        public int id;//订单号
        public string commodity;//商品名称
        public string client;//客户
        public int money;//金额
        public override bool Equals(object obj)
        {
            if(obj is Order)
            {
                Order m = (Order)obj;
                if (m.id == id && m.commodity == commodity && m.client == client && m.money == money)
                    return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(id, commodity, client, money);
        }

        public override string ToString()
        {
            return id + " " + commodity + " " + client + " " + money;
        }
    }
    public class OrderDetails:Order
    {
        public double date, time;
        public int num;//下单日期，下单时间，商品数量
        public override bool Equals(object obj)
        {
            if (obj is OrderDetails)
            {
                OrderDetails m = (OrderDetails)obj;
                if (m.date == date && m.time == time && m.num == num)
                    return base.Equals(obj);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(base.GetHashCode(), id, commodity, client, money, date, time, num);
        }

        public override string ToString()
        {
            return base.ToString()+" "+date + " " + time + " " + num;
        }
    }
    public class OrderService
    {
        List<OrderDetails> list = new List<OrderDetails>();
         public void add(OrderDetails order)
        {
            foreach( var a in list)
            {
                if (a.Equals(order) == true)
                {
                    Console.WriteLine("已有重复的订单，不能继续添加");
                    return;
                }
            }
            list.Add(order);
        }
        public bool search1(int id)
        {
            var order = from m in list where m.id == id orderby m.money select m;
            if (order.Count() == 0) 
                return false;
            else
            {
                foreach (OrderDetails n in order)
                {
                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
       public bool search2(string commodity)
        {
            var order = from m in list where m.commodity == commodity orderby m.money select m;
            if (order.Count() == 0) return false;
            else
            {
                foreach (OrderDetails n in order)
                {
                    
                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
        public bool search3(string client)
        {
            var order = from m in list where m.client == client orderby m.money select m;
            if (order.Count() == 0) return false;
            else
            {
                foreach (OrderDetails n in order)
                {
                    
                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
       public bool search4(int money)
        {
            var order = from m in list where m.money == money orderby m.money select m;
            if (order.Count()==0) return false;
            else { 
                foreach(OrderDetails n in order)
                {
                    
                    Console.WriteLine(n.ToString());
                }
                return true; 
            }
        }
       public bool remove(OrderDetails order)
        {   bool flag= false;
            OrderDetails od = null;
            foreach(OrderDetails n in list)
            {
                if (n.id == order.id)
                {
                    od = n;
                    flag = true;
                }
            }
            if (flag)
                list.Remove(od);
            return flag;
        }
       public bool modify(OrderDetails order)
        {
            foreach(OrderDetails n in list)
            {
                if (n.id == order.id)
                {
                    n.commodity = order.commodity;
                    n.client = order.client;
                    n.money = order.money;
                    n.date = order.date;
                    n.time = order.time;
                    n.num = order.num;
                    return true;
                }
            }
            return false;
        }
        public void view()
        {
            foreach(OrderDetails n in list)
            {
                Console.WriteLine(n.ToString());
            }
        }
        public void sort1()
        {
            var m = list.OrderBy(s => s.id);
            list = m.ToList();
        }
        public void sort2()
        {
            var m=list.OrderByDescending(s=>s.id);
            list = m.ToList();
        }
        public void sort3()
        {
            var m = list.OrderBy(s => s.money);
            list = m.ToList();
        }
        public void sort4()
        {
            var m = list.OrderByDescending(s => s.money);
            list = m.ToList();
        }
        public void sort5()
        {
            var m = list.OrderByDescending(s => s.date);
            list = m.ToList();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("订单管理程序");
            Console.WriteLine("1.添加订单");
            Console.WriteLine("2.删除订单");
            Console.WriteLine("3.修改订单");
            Console.WriteLine("4.按订单号查询订单");
            Console.WriteLine("5.按商品名称查询订单");
            Console.WriteLine("6.按客户查询订单");
            Console.WriteLine("7.按订单金额查询订单");
            Console.WriteLine("8.查看所有订单");
            Console.WriteLine("9.按订单号降序排序");
            Console.WriteLine("10.按订单日期降序排序");
            Console.WriteLine("11.按订单金额升序排序");
            Console.WriteLine("12.按订单金额降序排序");
            Console.WriteLine("13.按订单号升序排序");
            Console.WriteLine("0.退出程序");
            OrderService service=new OrderService();
            int flag = 0;
            while (flag==0)
            {
                int sel;
                Console.WriteLine("请输入操作：");
                sel=Convert.ToInt32(Console.ReadLine());
                OrderDetails ord=new OrderDetails();
                switch (sel) {
                    case 1:
                        Console.WriteLine("请输入添加的订单信息（订单号、商品名称、客户、订单金额、订单日期、订单时间、商品数量）");
                        ord.id = Convert.ToInt32(Console.ReadLine());
                        ord.commodity = Console.ReadLine();
                        ord.client = Console.ReadLine();
                        ord.money = Convert.ToInt32(Console.ReadLine());
                        ord.date = Convert.ToDouble(Console.ReadLine());
                        ord.time = Convert.ToDouble(Console.ReadLine());
                        ord.num = Convert.ToInt32(Console.ReadLine());
                        service.add(ord);
                        break;
                    case 2:
                        Console.WriteLine("请输入删除的订单信息（订单号、商品名称、客户、订单金额、订单日期、订单时间、商品数量）");
                        ord.id = Convert.ToInt32(Console.ReadLine());
                        ord.commodity = Console.ReadLine();
                        ord.client = Console.ReadLine();
                        ord.money = Convert.ToInt32(Console.ReadLine());
                        ord.date = Convert.ToDouble(Console.ReadLine());
                        ord.time = Convert.ToDouble(Console.ReadLine());
                        ord.num = Convert.ToInt32(Console.ReadLine());
                        if (service.remove(ord)==true)
                            Console.WriteLine("删除成功");
                        else Console.WriteLine("未找到订单，删除失败");
                        break;
                    case 3:
                        Console.WriteLine("请输入修改的订单信息（订单号、修改后的商品名称、客户、订单金额、订单日期、订单时间、商品数量）");
                        ord.id = Convert.ToInt32(Console.ReadLine());
                        ord.commodity = Console.ReadLine();
                        ord.client = Console.ReadLine();
                        ord.money = Convert.ToInt32(Console.ReadLine());
                        ord.date = Convert.ToDouble(Console.ReadLine());
                        ord.time = Convert.ToDouble(Console.ReadLine());
                        ord.num = Convert.ToInt32(Console.ReadLine());
                        if (service.modify(ord)==true)
                            Console.WriteLine("修改成功");
                        else Console.WriteLine("未找到订单，修改失败");
                        break;
                    case 4:
                        Console.WriteLine("按订单号查询（请输入订单号）");
                        int id = Convert.ToInt32(Console.ReadLine());
                        service.search1(id);
                        break;
                    case 5:
                        Console.WriteLine("按商品名称查询（请输入商品名称）");
                        string str = Console.ReadLine();
                        service.search2(str);
                        break;
                    case 6:
                        Console.WriteLine("按客户查询（请输入客户名）");
                        string str1 = Console.ReadLine();
                        service.search2(str1);
                        break;
                    case 7:
                        Console.WriteLine("按订单金额查询（请输入订单金额）");
                        int money = Convert.ToInt32(Console.ReadLine());
                        service.search4(money);
                        break;
                    case 8:
                        Console.WriteLine("查看所有订单：");
                        service.view();
                        break;
                    case 9:
                        service.sort2();
                        Console.WriteLine("已将订单按订单号降序排序");
                        break;
                    case 10:
                        service.sort5();
                        Console.WriteLine("已将订单按日期降序排序");
                        break;
                    case 11:
                        service.sort3();
                        Console.WriteLine("已将订单按金额升序排序");
                        break;
                    case 12:
                        service.sort4();
                        Console.WriteLine("已将订单按金额降序排序");
                        break;
                    case 13:
                        service.sort1();
                        Console.WriteLine("已将订单按订单号升序排序");
                        break;
                    case 0:
                        flag=1;
                        break;
                    default:
                        Console.WriteLine("操作数有误");
                        break;
                }
            }
        }
    }
}
